<!DOCTYPE html>
<html dir="ltr" lang="zh">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<meta content='1. 说说什么是MyBatis? # Mybatis 是一个半 自动ORM（对象关系映射）框架，它内部封装了 JDBC，开发时只需要关注 SQL 语句本身，不需要花费精力去处理加载驱动、创建连接、创建statement 等繁杂的过程。 MyBatis 可以使用 XML 或注解来配置和映射原生信息，将 POJO 映射成数据库中的记录，避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 2.为什么说Mybatis是半自动ORM映射工具？它与全自动的区别在哪里？ # Hibernate属于全自动ORM映射工具，使用Hibernate查询关联对象或者关联集合对象时，可以根据对象关系模型直接获取，所以它是全自动的。 而Mybatis在查询关联对象或关联集合对象时，需要手动编写SQL来完成，所以，被称之为半自动ORM映射工具。 3.Hibernate 和 MyBatis 有什么区别？ # 相同点
都是对jdbc的封装，都是应用于持久层的框架。 不同点
1）映射关系
MyBatis 是一个半自动映射的框架，配置Java对象与sql语句执行结果的对应关系，多表关联关系配置简单 Hibernate 是一个全表映射的框架，配置Java对象与数据库表的对应关系，多表关联关系配置复杂 2）SQL优化和移植性
Hibernate 对SQL语句封装，提供了日志、缓存、级联（级联比 MyBatis 强大）等特性，此外还提供 HQL（Hibernate Query Language）操作数据库，数据库无关性支持好，但会多消耗性能。如果项目需要支持多种数据库，代码开发量少，但SQL语句优化困难。 MyBatis 需要手动编写 SQL，支持动态 SQL、处理列表、动态生成表名、支持存储过程。开发工作量相对大些。直接使用SQL语句操作数据库，不支持数据库无关性，但sql语句优化容易。 3）MyBatis和Hibernate的适用场景不同
Hibernate 是标准的ORM框架，SQL编写量较少，但不够灵活，适合于需求相对稳定，中小型的软件项目，比如：办公自动化系统 MyBatis 是半ORM框架，需要编写较多SQL，但是比较灵活，适合于需求变化频繁，快速迭代的项目，比如：电商网站 3. MyBatis使用过程？生命周期？ # MyBatis基本使用的过程大概可以分为这么几步：
1）创建SqlSessionFactory
可以从配置或者直接编码来创建SqlSessionFactory
String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().' name="description"/>
<meta content="#FFFFFF" name="theme-color"/>
<meta content="light dark" name="color-scheme"/><meta content="MyBatis" property="og:title"/>
<meta content='1. 说说什么是MyBatis? # Mybatis 是一个半 自动ORM（对象关系映射）框架，它内部封装了 JDBC，开发时只需要关注 SQL 语句本身，不需要花费精力去处理加载驱动、创建连接、创建statement 等繁杂的过程。 MyBatis 可以使用 XML 或注解来配置和映射原生信息，将 POJO 映射成数据库中的记录，避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。 2.为什么说Mybatis是半自动ORM映射工具？它与全自动的区别在哪里？ # Hibernate属于全自动ORM映射工具，使用Hibernate查询关联对象或者关联集合对象时，可以根据对象关系模型直接获取，所以它是全自动的。 而Mybatis在查询关联对象或关联集合对象时，需要手动编写SQL来完成，所以，被称之为半自动ORM映射工具。 3.Hibernate 和 MyBatis 有什么区别？ # 相同点
都是对jdbc的封装，都是应用于持久层的框架。 不同点
1）映射关系
MyBatis 是一个半自动映射的框架，配置Java对象与sql语句执行结果的对应关系，多表关联关系配置简单 Hibernate 是一个全表映射的框架，配置Java对象与数据库表的对应关系，多表关联关系配置复杂 2）SQL优化和移植性
Hibernate 对SQL语句封装，提供了日志、缓存、级联（级联比 MyBatis 强大）等特性，此外还提供 HQL（Hibernate Query Language）操作数据库，数据库无关性支持好，但会多消耗性能。如果项目需要支持多种数据库，代码开发量少，但SQL语句优化困难。 MyBatis 需要手动编写 SQL，支持动态 SQL、处理列表、动态生成表名、支持存储过程。开发工作量相对大些。直接使用SQL语句操作数据库，不支持数据库无关性，但sql语句优化容易。 3）MyBatis和Hibernate的适用场景不同
Hibernate 是标准的ORM框架，SQL编写量较少，但不够灵活，适合于需求相对稳定，中小型的软件项目，比如：办公自动化系统 MyBatis 是半ORM框架，需要编写较多SQL，但是比较灵活，适合于需求变化频繁，快速迭代的项目，比如：电商网站 3. MyBatis使用过程？生命周期？ # MyBatis基本使用的过程大概可以分为这么几步：
1）创建SqlSessionFactory
可以从配置或者直接编码来创建SqlSessionFactory
String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().' property="og:description"/>
<meta content="article" property="og:type"/>
<meta content="https://helloputong.gitee.io/notes/java-%E7%9B%B8%E5%85%B3/mybatis/" property="og:url"/><meta content="notes" property="article:section"/>
<title>MyBatis | Hello! 噗通 🍀</title>
<link href="/manifest.json" rel="manifest"/>
<link href="/favicon.png" rel="icon" type="image/x-icon"/>
<link crossorigin="anonymous" href="/book.min.a82d7e77ceb134d151c4d7e381eeb30623fbd5a524d58c584d8716ecec0205bd.css" integrity="sha256-qC1+d86xNNFRxNfjge6zBiP71aUk1YxYTYcW7OwCBb0=" rel="stylesheet"/>
<script defer="" src="/flexsearch.min.js"></script>
<script crossorigin="anonymous" defer="" integrity="sha256-+pR/j4Voa/VXQmH38FekcfPx1IEWD5WAkNOJruKNmQk=" src="/zh.search.min.fa947f8f85686bf5574261f7f057a471f3f1d481160f958090d389aee28d9909.js"></script>
<script crossorigin="anonymous" defer="" integrity="sha256-b2+Q/LjrHEnsOJg45rgB0N4ZQwuOUWkC+NdcPIvZhzk=" src="/sw.min.6f6f90fcb8eb1c49ec389838e6b801d0de19430b8e516902f8d75c3c8bd98739.js"></script>
<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->
</head>
<body dir="ltr">
<input class="hidden toggle" id="menu-control" type="checkbox"/>
<input class="hidden toggle" id="toc-control" type="checkbox"/>
<main class="container flex">
<aside class="book-menu">
<div class="book-menu-content">
<nav>
<h2 class="book-brand">
<a class="flex align-center" href="/"><span>Hello! 噗通 🍀</span>
</a>
</h2>
<div class="book-search">
<input aria-label="Search" data-hotkeys="s/" id="book-search-input" maxlength="64" placeholder="Search" type="text"/>
<div class="book-search-spinner hidden"></div>
<ul id="book-search-results"></ul>
</div>
<ul>
<li class="book-section-flat">
<span>--学习笔记--👇</span>
<ul>
<li>
<input class="toggle" id="section-62161c8b7eae8ea89aee3d6f310b2312" type="checkbox"/>
<label class="flex justify-between" for="section-62161c8b7eae8ea89aee3d6f310b2312">
<a class="" role="button">Android</a>
</label>
<ul>
<li>
<a class="" href="/notes/android/mac%E9%80%9A%E8%BF%87homebrew%E5%AE%89%E8%A3%85java8/">Mac 通过 Homebrew 安装 Java8</a>
</li>
<li>
<a class="" href="/notes/android/%E6%B4%BB%E5%8A%A8%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/">活动的生命周期</a>
</li>
<li>
<a class="" href="/notes/android/%E5%B8%B8%E7%94%A8%E6%8E%A7%E4%BB%B6%E5%92%8C4%E7%A7%8D%E5%B8%83%E5%B1%80/">常用控件和4种布局</a>
</li>
<li>
<a class="" href="/notes/android/android-%E4%B8%AD%E4%BA%8B%E4%BB%B6%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/">Android 中事件学习总结</a>
</li>
<li>
<a class="" href="/notes/android/android-%E4%B8%AD%E7%9A%84%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6%E5%92%8C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1/">Android 中的消息机制和异步任务</a>
</li>
<li>
<a class="" href="/notes/android/listview%E4%B8%AD%E5%85%B3%E4%BA%8E%E6%95%B0%E6%8D%AE%E6%9B%B4%E6%96%B0%E7%9A%84%E4%B8%A4%E4%B8%AA%E9%97%AE%E9%A2%98/">List View中关于数据更新的两个问题</a>
</li>
<li>
<a class="" href="/notes/android/spinner-%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/">Spinner 基本使用</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-d1dc8d9746f5c776e8a82499bbb2e7c6" type="checkbox"/>
<label class="flex justify-between" for="section-d1dc8d9746f5c776e8a82499bbb2e7c6">
<a class="" role="button">BMS</a>
</label>
<ul>
<li>
<a class="" href="/notes/bms/bms-%E7%9B%B8%E5%85%B3%E7%90%86%E8%AE%BA%E5%AD%A6%E4%B9%A0/">BMS 相关概念</a>
</li>
<li>
<a class="" href="/notes/bms/%E6%BC%94%E7%A4%BA%E5%8F%82%E6%95%B0%E8%A7%A3%E8%AF%BB/">功能演示</a>
</li>
<li>
<a class="" href="/notes/bms/%E5%8E%9F%E7%90%86%E5%9B%BE%E8%A7%A3%E8%AF%BB/">原理图解读</a>
</li>
<li>
<a class="" href="/notes/bms/%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90/">源码解析</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-b7444509cb631180897a34f028407c2c" type="checkbox"/>
<label class="flex justify-between" for="section-b7444509cb631180897a34f028407c2c">
<a class="" role="button">设计模式</a>
</label>
<ul>
<li>
<a class="" href="/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/uml-%E5%9B%BE/">Uml 图</a>
</li>
<li>
<a class="" href="/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99/">设计原则</a>
</li>
<li>
<a class="" href="/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E5%88%9B%E5%BB%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F/">创建型模式</a>
</li>
<li>
<a class="" href="/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E7%BB%93%E6%9E%84%E5%9E%8B%E6%A8%A1%E5%BC%8F/">结构型模式</a>
</li>
<li>
<a class="" href="/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E8%A1%8C%E4%B8%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F/">行为型模式</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-4364152b7ab5995d509c0b7b811005c4" type="checkbox"/>
<label class="flex justify-between" for="section-4364152b7ab5995d509c0b7b811005c4">
<a class="" role="button">JVM</a>
</label>
<ul>
<li>
<a class="" href="/notes/jvm/%E4%BB%80%E4%B9%88%E6%98%AF-jvm/">什么是 Jvm</a>
</li>
<li>
<a class="" href="/notes/jvm/%E7%A8%8B%E5%BA%8F%E8%AE%A1%E6%95%B0%E5%99%A8/">程序计数器</a>
</li>
<li>
<a class="" href="/notes/jvm/%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88/">虚拟机栈</a>
</li>
<li>
<a class="" href="/notes/jvm/%E6%9C%AC%E5%9C%B0%E6%96%B9%E6%B3%95%E6%A0%88-/">本地方法栈</a>
</li>
<li>
<a class="" href="/notes/jvm/%E5%A0%86/">堆</a>
</li>
<li>
<a class="" href="/notes/jvm/%E6%96%B9%E6%B3%95%E5%8C%BA/">方法区</a>
</li>
<li>
<a class="" href="/notes/jvm/%E7%9B%B4%E6%8E%A5%E5%86%85%E5%AD%98/">直接内存</a>
</li>
<li>
<a class="" href="/notes/jvm/%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6/">垃圾回收</a>
</li>
<li>
<a class="" href="/notes/jvm/%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E5%99%A8/">垃圾回收器</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-61661238f18c0095524962a5d1d6e676" type="checkbox"/>
<label class="flex justify-between" for="section-61661238f18c0095524962a5d1d6e676">
<a class="" role="button">Spring</a>
</label>
<ul>
<li>
<a class="" href="/notes/spring/beanfactory%E4%B8%8Eapplicationcontext/">Bean Factory与 Application Context</a>
</li>
<li>
<a class="" href="/notes/spring/nacos-%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83/">Nacos 配置中心</a>
</li>
<li>
<a class="" href="/notes/spring/open-feign-%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8%E7%A4%BA%E4%BE%8B/">Open Feign 远程调用示例</a>
</li>
<li>
<a class="" href="/notes/spring/springboot-%E6%95%B4%E5%90%88-mybatis-plus/">Spring Boot 整合 My Batis Plus</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-39abd0d44427d4a54e694a2b3f22d967" type="checkbox"/>
<label class="flex justify-between" for="section-39abd0d44427d4a54e694a2b3f22d967">
<a class="" role="button">TensorFlow</a>
</label>
<ul>
<li>
<a class="" href="/notes/tensorflow/tensorflow-%E7%AC%94%E8%AE%B0%E4%B8%80/">Tensor Flow 笔记（一）</a>
</li>
<li>
<a class="" href="/notes/tensorflow/tensorflow-%E7%AC%94%E8%AE%B0%E4%BA%8C/">Tensor Flow 笔记（二）</a>
</li>
<li>
<a class="" href="/notes/tensorflow/tensorflow-%E7%AC%94%E8%AE%B0%E4%B8%89/">Tensor Flow 笔记（三）</a>
</li>
<li>
<a class="" href="/notes/tensorflow/tensorflow-%E7%AC%94%E8%AE%B0%E5%9B%9B/">Tensor Flow 笔记（四）</a>
</li>
<li>
<a class="" href="/notes/tensorflow/tensorflow-%E7%AC%94%E8%AE%B0%E4%BA%94/">Tensor Flow 笔记（五）</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-e2ca0d138d67d9d3ae55da25ac044829" type="checkbox"/>
<label class="flex justify-between" for="section-e2ca0d138d67d9d3ae55da25ac044829">
<a class="" role="button">Redis</a>
</label>
<ul>
<li>
<a class="" href="/notes/redis/nosql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%AE%80%E4%BB%8B/">No Sql数据库简介</a>
</li>
<li>
<a class="" href="/notes/redis/redis-key/">Redis Key</a>
</li>
<li>
<a class="" href="/notes/redis/redis-string/">Redis String</a>
</li>
<li>
<a class="" href="/notes/redis/redis-list/">Redis List</a>
</li>
<li>
<a class="" href="/notes/redis/redis-set/">Redis Set</a>
</li>
<li>
<a class="" href="/notes/redis/redis-hash/">Redis Hash</a>
</li>
<li>
<a class="" href="/notes/redis/redis-zset/">Redis Zset</a>
</li>
<li>
<a class="" href="/notes/redis/redis-%E5%8F%91%E5%B8%83%E4%B8%8E%E8%AE%A2%E9%98%85/">Redis 发布与订阅</a>
</li>
<li>
<a class="" href="/notes/redis/redis-jedis/">Redis Jedis</a>
</li>
<li>
<a class="" href="/notes/redis/springboot-%E6%95%B4%E5%90%88-redis/">Spring Boot 整合 Redis</a>
</li>
<li>
<a class="" href="/notes/redis/redis-%E4%BA%8B%E5%8A%A1%E5%92%8C%E9%94%81%E6%9C%BA%E5%88%B6/">Redis 事务和锁机制</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-58f730a0b22fcdc7a886db614d77f88c" type="checkbox"/>
<label class="flex justify-between" for="section-58f730a0b22fcdc7a886db614d77f88c">
<a class="" role="button">代码随想录刷题</a>
</label>
<ul>
<li>
<a class="" href="/notes/leetcode/day001-%E7%AC%AC%E4%B8%80%E7%AB%A0%E6%95%B0%E7%BB%84/">Day001 第一章数组</a>
</li>
<li>
<a class="" href="/notes/leetcode/day002-%E7%AC%AC%E4%B8%80%E7%AB%A0%E6%95%B0%E7%BB%84/">Day002 第一章数组</a>
</li>
<li>
<a class="" href="/notes/leetcode/day003-%E7%AC%AC%E4%BA%8C%E7%AB%A0%E9%93%BE%E8%A1%A8/">Day003 第二章链表</a>
</li>
<li>
<a class="" href="/notes/leetcode/day004-%E7%AC%AC%E4%BA%8C%E7%AB%A0%E9%93%BE%E8%A1%A8/">Day004 第二章链表</a>
</li>
<li>
<a class="" href="/notes/leetcode/day006-%E7%AC%AC%E4%B8%89%E7%AB%A0%E5%93%88%E5%B8%8C%E8%A1%A8/">Day006 第三章哈希表</a>
</li>
<li>
<a class="" href="/notes/leetcode/day007-%E7%AC%AC%E4%B8%89%E7%AB%A0%E5%93%88%E5%B8%8C%E8%A1%A8/">Day007 第三章哈希表</a>
</li>
<li>
<a class="" href="/notes/leetcode/day008-%E7%AC%AC%E5%9B%9B%E7%AB%A0%E5%AD%97%E7%AC%A6%E4%B8%B2/">Day008 第四章字符串</a>
</li>
<li>
<a class="" href="/notes/leetcode/day009-%E7%AC%AC%E5%9B%9B%E7%AB%A0%E5%AD%97%E7%AC%A6%E4%B8%B2/">Day009 第四章字符串</a>
</li>
<li>
<a class="" href="/notes/leetcode/day010-%E7%AC%AC%E4%BA%94%E7%AB%A0%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">Day010 第五章栈与队列</a>
</li>
<li>
<a class="" href="/notes/leetcode/day011-%E7%AC%AC%E4%BA%94%E7%AB%A0%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">Day011 第五章栈与队列</a>
</li>
<li>
<a class="" href="/notes/leetcode/day013-%E7%AC%AC%E4%BA%94%E7%AB%A0%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">Day013 第五章栈与队列</a>
</li>
<li>
<a class="" href="/notes/leetcode/day014-%E7%AC%AC%E5%85%AD%E7%AB%A0%E4%BA%8C%E5%8F%89%E6%A0%91/">Day014 第六章二叉树</a>
</li>
<li>
<a class="" href="/notes/leetcode/day015-%E7%AC%AC%E5%85%AD%E7%AB%A0%E4%BA%8C%E5%8F%89%E6%A0%91/">Day015 第六章二叉树</a>
</li>
<li>
<a class="" href="/notes/leetcode/day017-%E7%AC%AC%E5%85%AD%E7%AB%A0%E4%BA%8C%E5%8F%89%E6%A0%91/">Day017 第六章二叉树</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-4f95435d3a74007e2c985ea455bbb6e6" type="checkbox"/>
<label class="flex justify-between" for="section-4f95435d3a74007e2c985ea455bbb6e6">
<a class="" role="button">MyBatisPlus</a>
</label>
<ul>
<li>
<a class="" href="/notes/mybatisplus/%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/">MP快速入门</a>
</li>
<li>
<a class="" href="/notes/mybatisplus/%E4%B8%80%E8%88%AC%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C/">一般查询操作</a>
</li>
<li>
<a class="" href="/notes/mybatisplus/%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2/">分页查询</a>
</li>
<li>
<a class="" href="/notes/mybatisplus/%E9%80%BB%E8%BE%91%E5%88%A0%E9%99%A4/">逻辑删除</a>
</li>
<li>
<a class="" href="/notes/mybatisplus/%E6%9D%A1%E4%BB%B6%E6%9F%A5%E8%AF%A2/">条件查询</a>
</li>
<li>
<a class="" href="/notes/mybatisplus/%E5%B0%81%E8%A3%85service%E4%BD%BF%E7%94%A8/">封装service使用</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-3907b2cf55ed520ad784e24525c8baa4" type="checkbox"/>
<label class="flex justify-between" for="section-3907b2cf55ed520ad784e24525c8baa4">
<a class="" role="button">Swift</a>
</label>
<ul>
<li>
<a class="" href="/notes/swift/swiftui-%E5%B8%B8%E7%94%A8%E8%A7%86%E5%9B%BE-views/">Swift Ui 常用视图 Views</a>
</li>
<li>
<a class="" href="/notes/swift/swiftui-%E4%BF%A1%E6%81%AF%E8%A7%86%E5%9B%BE-views/">Swift Ui 信息视图 Views</a>
</li>
<li>
<a class="" href="/notes/swift/swiftui-%E5%B1%82%E7%BA%A7%E8%A7%86%E5%9B%BE-views/">Swift Ui 层级视图 Views</a>
</li>
<li>
<a class="" href="/notes/swift/swiftui-%E6%8E%A7%E5%88%B6%E8%A7%86%E5%9B%BE-views/">Swift Ui 控制视图 Views</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-0f70934a6e5284fbc93928c61dfe9c83" type="checkbox"/>
<label class="flex justify-between" for="section-0f70934a6e5284fbc93928c61dfe9c83">
<a class="" role="button">Java</a>
</label>
<ul>
<li>
<a class="" href="/notes/java/arraylist-%E6%89%A9%E5%AE%B9%E8%A7%84%E5%88%99/">Array List 扩容规则</a>
</li>
<li>
<a class="" href="/notes/java/hashmap-%E7%9B%B8%E5%85%B3%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/">Hash Map 相关学习总结</a>
</li>
<li>
<a class="" href="/notes/java/java-%E5%8F%8D%E5%B0%84/">Java 反射</a>
</li>
</ul>
</li>
<li>
<input checked="" class="toggle" id="section-3d1ea9814960db7e996773b67773e705" type="checkbox"/>
<label class="flex justify-between" for="section-3d1ea9814960db7e996773b67773e705">
<a class="" role="button">Java八股</a>
</label>
<ul>
<li>
<a class="" href="/notes/java-%E7%9B%B8%E5%85%B3/%E5%B9%B6%E5%8F%91/">Concurrence</a>
</li>
<li>
<a class="active" href="/notes/java-%E7%9B%B8%E5%85%B3/mybatis/">MyBatis</a>
</li>
<li>
<a class="" href="/notes/java-%E7%9B%B8%E5%85%B3/mysql/">MySQL</a>
</li>
<li>
<a class="" href="/notes/java-%E7%9B%B8%E5%85%B3/jvm/">Jvm</a>
</li>
<li>
<a class="" href="/notes/java-%E7%9B%B8%E5%85%B3/redis/">Redis</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-ea55243c547fa272027885b73a74852e" type="checkbox"/>
<label class="flex justify-between" for="section-ea55243c547fa272027885b73a74852e">
<a class="" role="button">嵌入式重点总结</a>
</label>
<ul>
<li>
<a class="" href="/notes/%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%80%BB%E7%BB%93/c/">C 语言相关</a>
</li>
<li>
<a class="" href="/notes/%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%80%BB%E7%BB%93/%E4%B8%AD%E6%96%AD%E7%B3%BB%E7%BB%9F/">中断系统</a>
</li>
<li>
<a class="" href="/notes/%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%80%BB%E7%BB%93/%E5%AE%9A%E6%97%B6%E5%99%A8/">TIM</a>
</li>
<li>
<a class="" href="/notes/%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%80%BB%E7%BB%93/adc/">ADC</a>
</li>
<li>
<a class="" href="/notes/%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%80%BB%E7%BB%93/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/">嵌入式知识点串烧</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="book-section-flat">
<span>--解决方案--👇</span>
<ul>
<li>
<input class="toggle" id="section-f1d4602254471b9d0da445dd468cd456" type="checkbox"/>
<label class="flex justify-between" for="section-f1d4602254471b9d0da445dd468cd456">
<a class="" role="button">环境配置</a>
</label>
<ul>
<li>
<a class="" href="/solution/environment/apple-m%E7%B3%BB%E5%88%97%E8%8A%AF%E7%89%87%E5%AE%89%E8%A3%85-pyqt/">Apple M系列芯片安装 Pyqt</a>
</li>
<li>
<a class="" href="/solution/environment/docker-%E5%AE%89%E8%A3%85-redis-/">Docker 安装 Redis</a>
</li>
<li>
<a class="" href="/solution/environment/hugo-%E4%B8%BB%E9%A2%98-hugo-book-%E4%B8%AD%E8%8B%B1%E6%96%87%E6%90%9C%E7%B4%A2%E9%85%8D%E7%BD%AE/">Hugo 主题 Hugo Book 中英文搜索配置</a>
</li>
<li>
<a class="" href="/solution/environment/iterm2-oh-my-zsh-%E9%85%8D%E7%BD%AE/">I Term2 Oh My Zsh 配置</a>
</li>
<li>
<a class="" href="/solution/environment/m1-%E8%8A%AF%E7%89%87-docker-%E5%AE%89%E8%A3%85-mysql5.7-/">M1 芯片 Docker 安装 Mysql5.7</a>
</li>
<li>
<a class="" href="/solution/environment/mac-idea-%E5%BF%AB%E6%8D%B7%E9%94%AE%E4%BD%8D/">MAC Idea 快捷键位</a>
</li>
<li>
<a class="" href="/solution/environment/mac-%E5%90%AF%E5%8A%A8%E5%8F%B0%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A1%8C%E5%88%97%E5%B8%83%E5%B1%80/">MAC 启动台自定义行列布局</a>
</li>
<li>
<a class="" href="/solution/environment/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a>
</li>
<li>
<a class="" href="/solution/environment/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E7%9A%84%E8%87%AA%E5%90%AF%E5%8A%A8%E8%AE%BE%E7%BD%AE/">内网穿透的自启动设置</a>
</li>
</ul>
</li>
<li>
<input class="toggle" id="section-5a76a664ba4855b79d3c1bc77e5b08b1" type="checkbox"/>
<label class="flex justify-between" for="section-5a76a664ba4855b79d3c1bc77e5b08b1">
<a class="" role="button">杂乱问题</a>
</label>
<ul>
<li>
<a class="" href="/solution/problems/brew-%E4%B8%80%E4%BA%9B%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">Brew 一些常用命令</a>
</li>
<li>
<a class="" href="/solution/problems/docker-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">Docker 常用命令</a>
</li>
<li>
<a class="" href="/solution/problems/git-github-%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4%E6%95%B4%E7%90%86/">Git Git Hub 相关命令整理</a>
</li>
<li>
<a class="" href="/solution/problems/%E9%9D%A2%E8%AF%95%E9%A2%98%E6%89%AB%E7%9B%B2/">面试题扫盲</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="book-section-flat">
<span>一点业余👇</span>
<ul>
</ul>
</li>
<li class="book-section-flat">
<span>--其他记录--👇</span>
<ul>
<li>
<input class="toggle" id="section-9ef4d2063ddc9af7785b193647f22260" type="checkbox"/>
<label class="flex justify-between" for="section-9ef4d2063ddc9af7785b193647f22260">
<a class="" role="button">我和阿刁</a>
</label>
<ul>
<li>
<a class="" href="/daily/ad/%E5%85%B3%E4%BA%8E%E9%98%BF%E5%88%812022%E5%B9%B4%E7%9A%84%E7%94%9F%E6%97%A5%E7%9A%84%E5%B0%8F%E8%AE%BA%E6%96%87/">关于阿刁2022年的生日的小论文</a>
</li>
<li>
<a class="" href="/daily/ad/%E5%85%B3%E4%BA%8E%E9%98%BF%E5%88%81%E7%9A%842021%E5%B9%B4%E5%BA%A6%E6%80%BB%E7%BB%93/">关于阿刁的2021年度总结</a>
</li>
<li>
<a class="" href="/daily/ad/%E5%85%B3%E4%BA%8E%E9%98%BF%E5%88%81%E7%9A%842022%E5%B9%B4%E5%BA%A6%E6%80%BB%E7%BB%93/">关于阿刁的2022年度总结</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
<script>(function(){var e=document.querySelector("aside .book-menu-content");addEventListener("beforeunload",function(){localStorage.setItem("menu.scrollTop",e.scrollTop)}),e.scrollTop=localStorage.getItem("menu.scrollTop")})()</script>
</div>
</aside>
<div class="book-page">
<header class="book-header">
<div class="flex align-center justify-between">
<label for="menu-control">
<img alt="Menu" class="book-icon" src="/svg/menu.svg"/>
</label>
<strong>MyBatis</strong>
<label for="toc-control">
<img alt="Table of Contents" class="book-icon" src="/svg/toc.svg"/>
</label>
</div>
<aside class="hidden clearfix">
<nav id="TableOfContents">
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#1-说说什么是mybatis">1. 说说什么是MyBatis?</a></li>
<li><a href="#2为什么说mybatis是半自动orm映射工具它与全自动的区别在哪里">2.为什么说Mybatis是半自动ORM映射工具？它与全自动的区别在哪里？</a></li>
<li><a href="#3hibernate-和-mybatis-有什么区别">3.Hibernate 和 MyBatis 有什么区别？</a></li>
<li><a href="#3-mybatis使用过程生命周期">3. MyBatis使用过程？生命周期？</a></li>
<li><a href="#4mybatis生命周期">4.MyBatis生命周期？</a></li>
<li><a href="#5-在mapper中如何传递多个参数">5. 在mapper中如何传递多个参数？</a></li>
<li><a href="#6-实体类属性名和表中字段名不一样-怎么办">6. 实体类属性名和表中字段名不一样 ，怎么办?</a></li>
<li><a href="#7mybatis是否可以映射enum枚举类">7.Mybatis是否可以映射Enum枚举类？</a></li>
<li><a href="#8和的区别">8.<code>#{}和${}的区别</code>?</a></li>
<li><a href="#9模糊查询like语句该怎么写">9.模糊查询like语句该怎么写?</a></li>
<li><a href="#10mybatis能执行一对一一对多的关联查询吗">10.Mybatis能执行一对一、一对多的关联查询吗？</a></li>
<li><a href="#11-如何获取生成的主键">11. 如何获取生成的主键?</a></li>
<li><a href="#12-mybatis支持动态sql吗">12. MyBatis支持动态SQL吗？</a></li>
<li><a href="#13-为什么mapper接口不需要实现类">13. 为什么Mapper接口不需要实现类？</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</aside>
</header>
<article class="markdown"><h3 id="1-说说什么是mybatis">
  1. 说说什么是MyBatis?
  <a class="anchor" href="#1-%e8%af%b4%e8%af%b4%e4%bb%80%e4%b9%88%e6%98%afmybatis">#</a>
</h3>
<ul>
<li>Mybatis 是一个半 自动ORM（对象关系映射）框架，它内部封装了 JDBC，开发时只需要关注 SQL 语句本身，不需要花费精力去处理加载驱动、创建连接、创建statement 等繁杂的过程。</li>
<li>MyBatis 可以使用 XML 或注解来配置和映射原生信息，将 POJO 映射成数据库中的记录，避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。</li>
</ul>
<h3 id="2为什么说mybatis是半自动orm映射工具它与全自动的区别在哪里">
  2.为什么说Mybatis是半自动ORM映射工具？它与全自动的区别在哪里？
  <a class="anchor" href="#2%e4%b8%ba%e4%bb%80%e4%b9%88%e8%af%b4mybatis%e6%98%af%e5%8d%8a%e8%87%aa%e5%8a%a8orm%e6%98%a0%e5%b0%84%e5%b7%a5%e5%85%b7%e5%ae%83%e4%b8%8e%e5%85%a8%e8%87%aa%e5%8a%a8%e7%9a%84%e5%8c%ba%e5%88%ab%e5%9c%a8%e5%93%aa%e9%87%8c">#</a>
</h3>
<ul>
<li>Hibernate属于全自动ORM映射工具，使用Hibernate查询关联对象或者关联集合对象时，可以根据对象关系模型直接获取，所以它是全自动的。</li>
<li>而Mybatis在查询关联对象或关联集合对象时，需要手动编写SQL来完成，所以，被称之为半自动ORM映射工具。</li>
</ul>
<h3 id="3hibernate-和-mybatis-有什么区别">
  3.Hibernate 和 MyBatis 有什么区别？
  <a class="anchor" href="#3hibernate-%e5%92%8c-mybatis-%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab">#</a>
</h3>
<p><strong>相同点</strong></p>
<ul>
<li>都是对jdbc的封装，都是应用于持久层的框架。</li>
</ul>
<p><strong>不同点</strong></p>
<p>1）映射关系</p>
<ul>
<li>MyBatis 是一个半自动映射的框架，配置Java对象与sql语句执行结果的对应关系，多表关联关系配置简单</li>
<li>Hibernate 是一个全表映射的框架，配置Java对象与数据库表的对应关系，多表关联关系配置复杂</li>
</ul>
<p>2）<strong>SQL优化和移植性</strong></p>
<ul>
<li>Hibernate 对SQL语句封装，提供了日志、缓存、级联（级联比 MyBatis 强大）等特性，此外还提供 HQL（Hibernate Query Language）操作数据库，数据库无关性支持好，但会多消耗性能。如果项目需要支持多种数据库，代码开发量少，但SQL语句优化困难。</li>
<li>MyBatis 需要手动编写 SQL，支持动态 SQL、处理列表、动态生成表名、支持存储过程。开发工作量相对大些。直接使用SQL语句操作数据库，不支持数据库无关性，但sql语句优化容易。</li>
</ul>
<p>3）<strong>MyBatis和Hibernate的适用场景不同</strong></p>
<ul>
<li>Hibernate 是标准的ORM框架，SQL编写量较少，但不够灵活，适合于需求相对稳定，中小型的软件项目，比如：办公自动化系统</li>
<li>MyBatis 是半ORM框架，需要编写较多SQL，但是比较灵活，适合于需求变化频繁，快速迭代的项目，比如：电商网站</li>
</ul>
<h3 id="3-mybatis使用过程生命周期">
  3. MyBatis使用过程？生命周期？
  <a class="anchor" href="#3-mybatis%e4%bd%bf%e7%94%a8%e8%bf%87%e7%a8%8b%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f">#</a>
</h3>
<p>MyBatis基本使用的过程大概可以分为这么几步：</p>
<ul>
<li>
<p>1）创建SqlSessionFactory</p>
<p>可以从配置或者直接编码来创建SqlSessionFactory</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span>String resource <span style="color:#f92672">=</span> <span style="color:#e6db74">"org/mybatis/example/mybatis-config.xml"</span><span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>InputStream inputStream <span style="color:#f92672">=</span> Resources<span style="color:#f92672">.</span><span style="color:#a6e22e">getResourceAsStream</span><span style="color:#f92672">(</span>resource<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>SqlSessionFactory sqlSessionFactory <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> SqlSessionFactoryBuilder<span style="color:#f92672">().</span><span style="color:#a6e22e">build</span><span style="color:#f92672">(</span>inputStream<span style="color:#f92672">);</span>
</span></span></code></pre></div></li>
<li>
<p>2）通过SqlSessionFactory创建SqlSession</p>
<p>SqlSession（会话）可以理解为程序和数据库之间的桥梁</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span>SqlSession session <span style="color:#f92672">=</span> sqlSessionFactory<span style="color:#f92672">.</span><span style="color:#a6e22e">openSession</span><span style="color:#f92672">();</span>
</span></span></code></pre></div></li>
<li>
<p>3）通过sqlsession执行数据库操作</p>
<p>可以通过 SqlSession 实例来直接执行已映射的 SQL 语句：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Blog blog <span style="color:#f92672">=</span> <span style="color:#f92672">(</span>Blog<span style="color:#f92672">)</span>session<span style="color:#f92672">.</span><span style="color:#a6e22e">selectOne</span><span style="color:#f92672">(</span><span style="color:#e6db74">"org.mybatis.example.BlogMapper.selectBlog"</span><span style="color:#f92672">,</span> <span style="color:#ae81ff">101</span><span style="color:#f92672">);</span>
</span></span></code></pre></div><p>更常用的方式是先获取Mapper(映射)，然后再执行SQL语句：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span>BlogMapper mapper <span style="color:#f92672">=</span> session<span style="color:#f92672">.</span><span style="color:#a6e22e">getMapper</span><span style="color:#f92672">(</span>BlogMapper<span style="color:#f92672">.</span><span style="color:#a6e22e">class</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>Blog blog <span style="color:#f92672">=</span> mapper<span style="color:#f92672">.</span><span style="color:#a6e22e">selectBlog</span><span style="color:#f92672">(</span><span style="color:#ae81ff">101</span><span style="color:#f92672">);</span>
</span></span></code></pre></div></li>
<li>
<p>4）调用session.commit()提交事务</p>
<p>如果是更新、删除语句，我们还需要提交一下事务。</p>
</li>
<li>
<p>5）调用session.close()关闭会话</p>
<p>最后一定要记得关闭会话。</p>
</li>
</ul>
<h3 id="4mybatis生命周期">
  4.MyBatis生命周期？
  <a class="anchor" href="#4mybatis%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f">#</a>
</h3>
<ul>
<li>
<p>SqlSessionFactoryBuilder：一旦创建了 SqlSessionFactory，就不再需要它了。 因此 SqlSessionFactoryBuilder 实例的生命周期只存在于方法的内部。</p>
</li>
<li>
<p>SqlSessionFactory：SqlSessionFactory 是用来创建SqlSession的，相当于一个数据库连接池，每次创建SqlSessionFactory都会使用数据库资源，多次创建和销毁是对资源的浪费。所以SqlSessionFactory是应用级的生命周期，而且应该是单例的。</p>
</li>
<li>
<p>SqlSession：SqlSession相当于JDBC中的Connection，<strong>SqlSession 的实例不是线程安全的，因此是不能被共享的，所以它的最佳的生命周期是一次请求或一个方法。</strong></p>
</li>
<li>
<p>Mapper：映射器是一些绑定映射语句的接口。映射器接口的实例是从 SqlSession 中获得的，它的生命周期在sqlsession事务方法之内，一般会控制在方法级。</p>
</li>
</ul>
<h3 id="5-在mapper中如何传递多个参数">
  5. 在mapper中如何传递多个参数？
  <a class="anchor" href="#5-%e5%9c%a8mapper%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bc%a0%e9%80%92%e5%a4%9a%e4%b8%aa%e5%8f%82%e6%95%b0">#</a>
</h3>
<ol>
<li>
<p>顺序传参法</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> User <span style="color:#a6e22e">selectUser</span><span style="color:#f92672">(</span>String name<span style="color:#f92672">,</span> <span style="color:#66d9ef">int</span> deptId<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;</span>select id<span style="color:#f92672">=</span><span style="color:#e6db74">"selectUser"</span> resultMap<span style="color:#f92672">=</span><span style="color:#e6db74">"UserResultMap"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    select <span style="color:#f92672">*</span> from user
</span></span><span style="display:flex;"><span>    where user_name <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span><span style="color:#ae81ff">0</span><span style="color:#f92672">}</span> and dept_id <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span><span style="color:#ae81ff">1</span><span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/</span>select<span style="color:#f92672">&gt;</span>
</span></span></code></pre></div><ul>
<li><code>\#{}</code>里面的数字代表传入参数的顺序。</li>
<li>这种方法不建议使用，sql层表达不直观，且一旦顺序调整容易出错。</li>
</ul>
</li>
<li>
<p>@Param注解传参法</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> User <span style="color:#a6e22e">selectUser</span><span style="color:#f92672">(</span><span style="color:#a6e22e">@Param</span><span style="color:#f92672">(</span><span style="color:#e6db74">"userName"</span><span style="color:#f92672">)</span> String name<span style="color:#f92672">,</span> <span style="color:#66d9ef">int</span> <span style="color:#a6e22e">@Param</span><span style="color:#f92672">(</span><span style="color:#e6db74">"deptId"</span><span style="color:#f92672">)</span> deptId<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;</span>select id<span style="color:#f92672">=</span><span style="color:#e6db74">"selectUser"</span> resultMap<span style="color:#f92672">=</span><span style="color:#e6db74">"UserResultMap"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    select <span style="color:#f92672">*</span> from user
</span></span><span style="display:flex;"><span>    where user_name <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span>userName<span style="color:#f92672">}</span> and dept_id <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span>deptId<span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/</span>select<span style="color:#f92672">&gt;</span>
</span></span></code></pre></div><ul>
<li><code>\#{}</code>里面的名称对应的是注解@Param括号里面修饰的名称。</li>
<li>这种方法在参数不多的情况还是比较直观的（推荐使用）。</li>
</ul>
</li>
<li>
<p>Map传参法</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> User <span style="color:#a6e22e">selectUser</span><span style="color:#f92672">(</span>Map<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">,</span> Object<span style="color:#f92672">&gt;</span> params<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;</span>select id<span style="color:#f92672">=</span><span style="color:#e6db74">"selectUser"</span> parameterType<span style="color:#f92672">=</span><span style="color:#e6db74">"java.util.Map"</span> resultMap<span style="color:#f92672">=</span><span style="color:#e6db74">"UserResultMap"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    select <span style="color:#f92672">*</span> from user
</span></span><span style="display:flex;"><span>    where user_name <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span>userName<span style="color:#f92672">}</span> and dept_id <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span>deptId<span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/</span>select<span style="color:#f92672">&gt;</span>
</span></span></code></pre></div><ul>
<li><code>\#{}</code>里面的名称对应的是Map里面的key名称。</li>
<li>这种方法适合传递多个参数，且参数易变能灵活传递的情况。</li>
</ul>
</li>
<li>
<p>Java Bean传参法</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> User <span style="color:#a6e22e">selectUser</span><span style="color:#f92672">(</span>User user<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;</span>select id<span style="color:#f92672">=</span><span style="color:#e6db74">"selectUser"</span> parameterType<span style="color:#f92672">=</span><span style="color:#e6db74">"com.jourwon.pojo.User"</span> resultMap<span style="color:#f92672">=</span><span style="color:#e6db74">"UserResultMap"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    select <span style="color:#f92672">*</span> from user
</span></span><span style="display:flex;"><span>    where user_name <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span>userName<span style="color:#f92672">}</span> and dept_id <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">#</span><span style="color:#f92672">{</span>deptId<span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/</span>select<span style="color:#f92672">&gt;</span>
</span></span></code></pre></div><ul>
<li><code>\#{}</code>里面的名称对应的是User类里面的成员属性。</li>
<li>这种方法直观，需要建一个实体类，扩展不容易，需要加属性，但代码可读性强，业务逻辑处理方便，推荐使用。（推荐使用）。</li>
</ul>
</li>
</ol>
<h3 id="6-实体类属性名和表中字段名不一样-怎么办">
  6. 实体类属性名和表中字段名不一样 ，怎么办?
  <a class="anchor" href="#6-%e5%ae%9e%e4%bd%93%e7%b1%bb%e5%b1%9e%e6%80%a7%e5%90%8d%e5%92%8c%e8%a1%a8%e4%b8%ad%e5%ad%97%e6%ae%b5%e5%90%8d%e4%b8%8d%e4%b8%80%e6%a0%b7-%e6%80%8e%e4%b9%88%e5%8a%9e">#</a>
</h3>
<ul>
<li>
<p>第1种： 通过在查询的SQL语句中定义字段名的别名，让字段名的别名和实体类的属性名一致。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"getOrder"</span> <span style="color:#a6e22e">parameterType=</span><span style="color:#e6db74">"int"</span> <span style="color:#a6e22e">resultType=</span><span style="color:#e6db74">"com.jourwon.pojo.Order"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>       select order_id id, order_no orderno ,order_price price form orders where order_id=#{id};
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div></li>
<li>
<p>第2种： 通过resultMap 中的<result>来映射字段名和实体类属性名的一一对应的关系。</result></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"getOrder"</span> <span style="color:#a6e22e">parameterType=</span><span style="color:#e6db74">"int"</span> <span style="color:#a6e22e">resultMap=</span><span style="color:#e6db74">"orderResultMap"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from orders where order_id=#{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#a6e22e">type=</span><span style="color:#e6db74">"com.jourwon.pojo.Order"</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"orderResultMap"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!–用id属性来映射主键字段–&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;id</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"id"</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"order_id"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!–用result属性来映射非主键字段，property为实体类属性名，column为数据库表中的属性–&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#a6e22e">property =</span><span style="color:#e6db74">"orderno"</span> <span style="color:#a6e22e">column =</span><span style="color:#e6db74">"order_no"</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"price"</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"order_price"</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span></code></pre></div></li>
</ul>
<h3 id="7mybatis是否可以映射enum枚举类">
  7.Mybatis是否可以映射Enum枚举类？
  <a class="anchor" href="#7mybatis%e6%98%af%e5%90%a6%e5%8f%af%e4%bb%a5%e6%98%a0%e5%b0%84enum%e6%9e%9a%e4%b8%be%e7%b1%bb">#</a>
</h3>
<ul>
<li>Mybatis当然可以映射枚举类，不单可以映射枚举类，Mybatis可以映射任何对象到表的一列上。映射方式为自定义一个TypeHandler，实现TypeHandler的setParameter()和getResult()接口方法。</li>
<li>TypeHandler有两个作用，一是完成从javaType至jdbcType的转换，二是完成jdbcType至javaType的转换，体现为setParameter()和getResult()两个方法，分别代表设置sql问号占位符参数和获取列查询结果。</li>
</ul>
<h3 id="8和的区别">
  8.<code>#{}和${}的区别</code>?
  <a class="anchor" href="#8%e5%92%8c%e7%9a%84%e5%8c%ba%e5%88%ab">#</a>
</h3>
<ul>
<li><code>#{}</code>是占位符，预编译处理；<code>${}</code>是拼接符，字符串替换，没有预编译处理。</li>
<li>Mybatis在处理<code>#{}</code>时，<code>#{}</code>传入参数是以字符串传入，会将SQL中的<code>#{}</code>替换为?号，调用PreparedStatement的set方法来赋值。</li>
<li><code>#{}</code> 可以有效的防止SQL注入，提高系统安全性；<code>${}</code> 不能防止SQL 注入</li>
<li><code>#{}</code> 的变量替换是在DBMS 中；<code>${}</code> 的变量替换是在 DBMS 外</li>
</ul>
<h3 id="9模糊查询like语句该怎么写">
  9.模糊查询like语句该怎么写?
  <a class="anchor" href="#9%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2like%e8%af%ad%e5%8f%a5%e8%af%a5%e6%80%8e%e4%b9%88%e5%86%99">#</a>
</h3>
<ul>
<li>1 ’<code>%${question}%</code>’ 可能引起SQL注入，不推荐</li>
<li>2 <code>"%"#{question}"%"</code> 注意：因为<code>#{…}</code>解析成sql语句时候，会在变量外侧自动加单引号’ ‘，所以这里 % 需要使用双引号" “，不能使用单引号 ’ ‘，不然会查不到任何结果。</li>
<li>3 <code>CONCAT('%',#{question},'%')</code> 使用CONCAT()函数，（推荐✨）</li>
<li>4 使用bind标签（不推荐）</li>
</ul>
<h3 id="10mybatis能执行一对一一对多的关联查询吗">
  10.Mybatis能执行一对一、一对多的关联查询吗？
  <a class="anchor" href="#10mybatis%e8%83%bd%e6%89%a7%e8%a1%8c%e4%b8%80%e5%af%b9%e4%b8%80%e4%b8%80%e5%af%b9%e5%a4%9a%e7%9a%84%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%e5%90%97">#</a>
</h3>
<p>当然可以，不止支持一对一、一对多的关联查询，还支持多对多、多对一的关联查询。</p>
<ul>
<li>
<p><strong>一对一<association></association></strong>: 比如订单和支付是一对一的关系，这种关联的实现：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// 实体类:
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Order</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> Integer orderId<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> String orderDesc<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * 支付对象
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> Pay pay<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//……
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">}</span>
</span></span></code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- # 结果映射 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- 订单resultMap --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"peopleResultMap"</span> <span style="color:#a6e22e">type=</span><span style="color:#e6db74">"cn.fighter3.entity.Order"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;id</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"orderId"</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"order_id"</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;result</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"orderDesc"</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"order_desc"</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!--一对一结果映射--&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;association</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"pay"</span> <span style="color:#a6e22e">javaType=</span><span style="color:#e6db74">"cn.fighter3.entity.Pay"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;id</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"payId"</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"pay_id"</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;result</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"account"</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"account"</span><span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/association&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span></code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- # 关联查询 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"getTeacher"</span> <span style="color:#a6e22e">resultMap=</span><span style="color:#e6db74">"getTeacherMap"</span> <span style="color:#a6e22e">parameterType=</span><span style="color:#e6db74">"int"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    select * from order o 
</span></span><span style="display:flex;"><span>     left join pay p on o.order_id=p.order_id
</span></span><span style="display:flex;"><span>    where  o.order_id=#{orderId}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div></li>
</ul>
<ul>
<li>
<p><strong>一对多<code>&lt;collection&gt;</code></strong>：比如商品分类和商品，是一对多的关系。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// 实体类
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Category</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> <span style="color:#66d9ef">int</span> categoryId<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">private</span> String categoryName<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    * 商品列表
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    **/</span>
</span></span><span style="display:flex;"><span>    List<span style="color:#f92672">&lt;</span>Product<span style="color:#f92672">&gt;</span> products<span style="color:#f92672">;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//……
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">}</span>
</span></span></code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- # 结果映射 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#a6e22e">type=</span><span style="color:#e6db74">"Category"</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"categoryBean"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;id</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"categoryId"</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"category_id"</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;result</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"categoryName"</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"category_name"</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- 一对多的关系 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">&lt;!-- property: 指的是集合属性的值, ofType：指的是集合中元素的类型 --&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;collection</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"products"</span> <span style="color:#a6e22e">ofType=</span><span style="color:#e6db74">"Product"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;id</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"product_id"</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"productId"</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;result</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"productName"</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"productName"</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">&lt;result</span> <span style="color:#a6e22e">column=</span><span style="color:#e6db74">"price"</span> <span style="color:#a6e22e">property=</span><span style="color:#e6db74">"price"</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/collection&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span></code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;!-- 关联查询分类和产品表 --&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"listCategory"</span> <span style="color:#a6e22e">resultMap=</span><span style="color:#e6db74">"categoryBean"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    select c.*, p.* from category_ c left join product_ p on c.id = p.cid
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>  
</span></span></code></pre></div></li>
</ul>
<h3 id="11-如何获取生成的主键">
  11. 如何获取生成的主键?
  <a class="anchor" href="#11-%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e7%94%9f%e6%88%90%e7%9a%84%e4%b8%bb%e9%94%ae">#</a>
</h3>
<ul>
<li>
<p>新增标签中添加：keyProperty=” ID " 即可</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;insert</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"insert"</span> <span style="color:#a6e22e">useGeneratedKeys=</span><span style="color:#e6db74">"true"</span> <span style="color:#a6e22e">keyProperty=</span><span style="color:#e6db74">"userId"</span> <span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    insert into user( 
</span></span><span style="display:flex;"><span>    user_name, user_password, create_time) 
</span></span><span style="display:flex;"><span>    values(#{userName}, #{userPassword} , #{createTime, jdbcType= TIMESTAMP})
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/insert&gt;</span>
</span></span></code></pre></div></li>
<li>
<p>这时候就可以完成回填主键</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-java" data-lang="java"><span style="display:flex;"><span>mapper<span style="color:#f92672">.</span><span style="color:#a6e22e">insert</span><span style="color:#f92672">(</span>user<span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>user<span style="color:#f92672">.</span><span style="color:#a6e22e">getId</span><span style="color:#f92672">;</span>
</span></span></code></pre></div></li>
</ul>
<h3 id="12-mybatis支持动态sql吗">
  12. MyBatis支持动态SQL吗？
  <a class="anchor" href="#12-mybatis%e6%94%af%e6%8c%81%e5%8a%a8%e6%80%81sql%e5%90%97">#</a>
</h3>
<p>MyBatis中有一些支持动态SQL的标签，它们的原理是从SQL参数对象中计算表达式的值，根据表达式的值动态拼接SQL，以此来完成动态SQL的功能。</p>
<ul>
<li>
<p>if：根据条件来组成where子句</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"findActiveBlogWithTitleLike"</span>
</span></span><span style="display:flex;"><span>   <span style="color:#a6e22e">resultType=</span><span style="color:#e6db74">"Blog"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>SELECT * FROM BLOG
</span></span><span style="display:flex;"><span>WHERE state = ‘ACTIVE’
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"title != null"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  AND title like #{title}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div></li>
<li>
<p>choose (when, otherwise)：这个和Java 中的 switch 语句有点像</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"findActiveBlogLike"</span>
</span></span><span style="display:flex;"><span>   <span style="color:#a6e22e">resultType=</span><span style="color:#e6db74">"Blog"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>SELECT * FROM BLOG WHERE state = ‘ACTIVE’
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;choose&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;when</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"title != null"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    AND title like #{title}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/when&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;when</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"author != null and author.name != null"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    AND author_name like #{author.name}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/when&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;otherwise&gt;</span>
</span></span><span style="display:flex;"><span>    AND featured = 1
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/otherwise&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/choose&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div></li>
<li>
<p><where>可以用在所有的查询条件都是动态的情况</where></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"findActiveBlogLike"</span>
</span></span><span style="display:flex;"><span>   <span style="color:#a6e22e">resultType=</span><span style="color:#e6db74">"Blog"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>SELECT * FROM BLOG
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;where&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"state != null"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>       state = #{state}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"title != null"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>      AND title like #{title}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"author != null and author.name != null"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>      AND author_name like #{author.name}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/where&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div></li>
<li>
<p><set> 可以用在动态更新的时候</set></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;update</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"updateAuthorIfNecessary"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  update Author
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;set&gt;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"username != null"</span><span style="color:#f92672">&gt;</span>username=#{username},<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"password != null"</span><span style="color:#f92672">&gt;</span>password=#{password},<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"email != null"</span><span style="color:#f92672">&gt;</span>email=#{email},<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">&lt;if</span> <span style="color:#a6e22e">test=</span><span style="color:#e6db74">"bio != null"</span><span style="color:#f92672">&gt;</span>bio=#{bio}<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/set&gt;</span>
</span></span><span style="display:flex;"><span>  where id=#{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/update&gt;</span>
</span></span></code></pre></div></li>
<li>
<p>foreach：看到名字就知道了，这个是用来循环的，可以对集合进行遍历</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;" tabindex="0"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#a6e22e">id=</span><span style="color:#e6db74">"selectPostIn"</span> <span style="color:#a6e22e">resultType=</span><span style="color:#e6db74">"domain.blog.Post"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>SELECT *
</span></span><span style="display:flex;"><span>FROM POST P
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;where&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;foreach</span> <span style="color:#a6e22e">item=</span><span style="color:#e6db74">"item"</span> <span style="color:#a6e22e">index=</span><span style="color:#e6db74">"index"</span> <span style="color:#a6e22e">collection=</span><span style="color:#e6db74">"list"</span>
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">open=</span><span style="color:#e6db74">"ID in ("</span> <span style="color:#a6e22e">separator=</span><span style="color:#e6db74">","</span> <span style="color:#a6e22e">close=</span><span style="color:#e6db74">")"</span> <span style="color:#a6e22e">nullable=</span><span style="color:#e6db74">"true"</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>        #{item}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/foreach&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/where&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div></li>
</ul>
<h3 id="13-为什么mapper接口不需要实现类">
  13. 为什么Mapper接口不需要实现类？
  <a class="anchor" href="#13-%e4%b8%ba%e4%bb%80%e4%b9%88mapper%e6%8e%a5%e5%8f%a3%e4%b8%8d%e9%9c%80%e8%a6%81%e5%ae%9e%e7%8e%b0%e7%b1%bb">#</a>
</h3>
<p><strong>动态代理</strong>。</p>
</article>
<footer class="book-footer">
<div class="flex flex-wrap justify-between">
</div>
<script>(function(){function e(e){const t=window.getSelection(),n=document.createRange();n.selectNodeContents(e),t.removeAllRanges(),t.addRange(n)}document.querySelectorAll("pre code").forEach(t=>{t.addEventListener("click",function(){if(window.getSelection().toString())return;e(t.parentElement),navigator.clipboard&&navigator.clipboard.writeText(t.parentElement.textContent)})})})()</script>
</footer>
<div class="book-comments">
</div>
<label class="hidden book-menu-overlay" for="menu-control"></label>
</div>
<aside class="book-toc">
<div class="book-toc-content">
<nav id="TableOfContents">
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#1-说说什么是mybatis">1. 说说什么是MyBatis?</a></li>
<li><a href="#2为什么说mybatis是半自动orm映射工具它与全自动的区别在哪里">2.为什么说Mybatis是半自动ORM映射工具？它与全自动的区别在哪里？</a></li>
<li><a href="#3hibernate-和-mybatis-有什么区别">3.Hibernate 和 MyBatis 有什么区别？</a></li>
<li><a href="#3-mybatis使用过程生命周期">3. MyBatis使用过程？生命周期？</a></li>
<li><a href="#4mybatis生命周期">4.MyBatis生命周期？</a></li>
<li><a href="#5-在mapper中如何传递多个参数">5. 在mapper中如何传递多个参数？</a></li>
<li><a href="#6-实体类属性名和表中字段名不一样-怎么办">6. 实体类属性名和表中字段名不一样 ，怎么办?</a></li>
<li><a href="#7mybatis是否可以映射enum枚举类">7.Mybatis是否可以映射Enum枚举类？</a></li>
<li><a href="#8和的区别">8.<code>#{}和${}的区别</code>?</a></li>
<li><a href="#9模糊查询like语句该怎么写">9.模糊查询like语句该怎么写?</a></li>
<li><a href="#10mybatis能执行一对一一对多的关联查询吗">10.Mybatis能执行一对一、一对多的关联查询吗？</a></li>
<li><a href="#11-如何获取生成的主键">11. 如何获取生成的主键?</a></li>
<li><a href="#12-mybatis支持动态sql吗">12. MyBatis支持动态SQL吗？</a></li>
<li><a href="#13-为什么mapper接口不需要实现类">13. 为什么Mapper接口不需要实现类？</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</aside>
</main>
</body>
</html>
